//
//  SlideController.m
//  MyTrip
//
//  Created by zhang on 15/8/26.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "SlideController.h"
#import "MBProgressHUD.h"

@interface SlideController ()<UIWebViewDelegate>

@property (nonatomic, retain) UIWebView *web;
@property (nonatomic, retain) MBProgressHUD *hud;
@property (nonatomic, retain) UIButton *backButton;


@end

@implementation SlideController

- (void)dealloc
{
    self.web.delegate = nil;
    [_backButton release];
    [_hud release];
    [_web release];
    [super dealloc];
}

- (void)createWeb{
    
    self.web = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height + 64 * OffHeight)] autorelease];
    [self.view addSubview:_web];
    _web.delegate = self;
    [_web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:70 / 255.0f green:160 / 255.0f blue:175 / 255.0f alpha:1];
    
    [self createWeb];
}

- (void)createBackButton{
    
    self.backButton = [[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25 * OffWidth, 15 * OffHeight)] autorelease];
    [_backButton setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"back222" ofType:@"png"]] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_backButton];
    
}

- (void)backAction:(id)sender{
    [_web goBack];
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, -64 * OffHeight, self.view.frame.size.width, self.view.frame.size.height + 64 * OffHeight)] autorelease];
//    [view setBackgroundColor:[UIColor blackColor]];
//    [self.navigationController.navigationBar addSubview:view];
//    [view setTag:10];
//    [view setAlpha:0.1];
    NSString *url = [NSString stringWithFormat:@"%@", request.URL];
    if (![url  isEqual: _url]) {
        self.tabBarController.tabBar.hidden = YES;
//        [self createBackButton];
    }else{
        if (_backButton) {
              [_backButton setImage:nil forState:UIControlStateNormal];
        }
//        self.tabBarController.tabBar.hidden = NO;
    }
    return YES;
}


- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    
    self.hud = [[MBProgressHUD alloc] init];
    [self.view addSubview:_hud];
    [_hud show:YES];
    _hud.labelText = @"正在拼命加载......";
    //    在网络请求之后
}

- (void)remover:(MBProgressHUD *)hud{
//    UIView *view = (UIView *)[self.navigationController.navigationBar viewWithTag:10];
//    hud = _hud;
//    if (view) {
//        [view removeFromSuperview];
//    }
    if (_hud.hidden == NO) {
        _hud.hidden = YES;
    }
    [_web stopLoading];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView{
//    UIView *view = (UIView *)[self.navigationController.navigationBar viewWithTag:10];
//    if (view) {
//        [view removeFromSuperview];
//    }
    if (_hud.hidden == NO) {
        _hud.hidden = YES;
    }
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
     [self performSelector:@selector(remover:) withObject:nil afterDelay:5];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
    if ([self  isViewLoaded] == YES  && self.view.window == nil ){
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
